package controller

import (
	"bytes"
	"encoding/json"
	bc "financial/application/blockchain"
	"financial/application/lib"
	"financial/application/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// 生产产品
func ProduceProduct(ctx *gin.Context)  {
	// 解析请求体
	req := new(lib.Product)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	//分配一个随机 ID 长度为  11+1
	req.Id = util.Krand(11, 0)
	req.Updated = time.Now()
	//将请求体序列化
	marshal, _ := json.Marshal(req)

	resp, err := bc.ChannelExecute("produceProduct", [][]byte{
		marshal,
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}

	// http返回
	ctx.JSON(http.StatusOK, resp)
}

func ProcessingProduct(ctx *gin.Context) {
	// 解析结构体
	req := new(lib.Product)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	req.Updated = time.Now()
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("processingProduct", [][]byte {
		marshal,
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

func TransportProduct(ctx *gin.Context) {
	// 解析结构体
	req := new(lib.Product)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	req.Updated = time.Now()
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("transportProduct", [][]byte {
		marshal,
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

func WholesaleProduct(ctx *gin.Context) {
	// 解析结构体
	req := new(lib.Product)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	req.Updated = time.Now()
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("wholesaleProduct", [][]byte {
		marshal,
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

func SalesProduct(ctx *gin.Context) {
	// 解析结构体
	req := new(lib.Product)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	req.Updated = time.Now()
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("salesProduct", [][]byte {
		marshal,
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

func BuyProduct(ctx *gin.Context) {
	// 解析结构体
	req := new(lib.Product)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	req.Updated = time.Now()
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("buyProduct", [][]byte {
		marshal,
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

func ProductList(ctx *gin.Context)  {
	resp, err := bc.ChannelQuery("getAllProduct", [][]byte {

	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.String(http.StatusOK, bytes.NewBuffer(resp.Payload).String())
}